﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) [INCLUDE APACHE 2.0 AS AN OPTION.]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Linq.Expressions;

namespace ppscd
{
    [Serializable]
    public class Replacement
    {
        public Replacement()
        {
        }

        public Replacement(string replace)
        {
            this.Replace = replace;
        }

        public Replacement(string replace, string with)
        {
            this.Replace = replace;
            this.With = with;
        }

        [XmlAttribute]
        public string Replace
        {
            get;
            set;
        }

        [XmlAttribute]
        public string With
        {
            get;
            set;
        }

        //public static bool TryReplace(string input, ref string output, List<Replacement> replacements)
        //{
        //    // TODO - refactor to linq
        //    foreach (var replacement in replacements)
        //    {
        //        if (input.Contains(replacement.Replace))
        //        {
        //            output = input.Replace(replacement.Replace, replacement.With);
        //            return true;
        //        }
        //    }

        //    return false;
        //}

        public static string ReplaceFirst(string old, List<Replacement> replacements)
        {
            // gracefully
            if (replacements == null)
            {
                return old;
            }
                
            foreach (var replacement in replacements)
            {
                if (old.Contains(replacement.Replace))
                {
                    // gracefully continue
                    if (string.IsNullOrEmpty(replacement.Replace))
                    {
                        continue;
                    }

                    return old.Replace(replacement.Replace, replacement.With);
                }
            }

            return old;
        }

        public static void R<T>(T t, Expression<Operation<T>> member, List<Replacement> replacements)
        {
            LambdaExpression lambda = member as LambdaExpression;

            var memberExpression = lambda.Body as MemberExpression;
            var memberInfo = memberExpression.Member;

            if (memberInfo.MemberType == MemberTypes.Property)
            {
                var propertyInfo = memberInfo as PropertyInfo;
                propertyInfo.SetValue(t, ReplaceFirst(propertyInfo.GetValue(t, null) as string, replacements), null);
            }

            //var memberInfo = (unary.Operand as MemberExpression).Member;

            

            
        }
    }

    public delegate object Operation();
    public delegate object Operation<T>(T declaringType);

}
